<?php

class ShoparticleController extends AppController {

	public $name = 'shoparticle';
	
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('categorieslist','categoriesadd','categoriesedit','categoriesdel','index','add','edit','del','uploadform');
	}
	
	public function categorieslist() {
		$query = array(
			'fields' => array('shoparticlecat.*'), 
		);
		$result = POP::exe('[@@:UM shoparticlecat][@@:QU find:all]',$query);	
        $this->set('result',$result);			
		
	}	
	
	public function categoriesadd() {        	
		if (!empty($this->data)) {
			$query = $this->data;
			if (POP::exe('[@@:UM shoparticlecat][@@:QU save]',$query)) {		
				$this->flash('更新成功！', Router::url('admin/shoparticle/categorieslist'));		
			} else {
				$this->flash('更新失败！', Router::url('admin/shoparticle/categorieslist'));
			}		 	
	    } 
		
	}	
	
	public function categoriesedit($id) {		
		if (!empty($this->data)) {
			$query['keyid'] = $id;
			$query['set']['shoparticlecat'] = $this->data;			
			if (POP::exe('[@@:UM shoparticlecat][@@:QU save]',$query)) {						
				$this->flash('更新成功！', Router::url('admin/shoparticle/categorieslist'));		
			} else {
				$this->flash('更新失败！', Router::url('admin/shoparticle/categorieslist'));
			}				 	
	    }else {
			$query = array(
				'conditions' => array('shoparticlecat.id' => $id), 
				'fields' => array('shoparticlecat.*'), 
			);
			$result = POP::exe('[@@:UM shoparticlecat][@@:QU find:first]',$query);		
            $this->set('result',$result['shoparticlecat']);

		}		
	}	
	
	public function categoriesdel($id) {
		$query = array(
			'conditions' => array('shoparticle.cat_id' => $id), 
			'fields' => array('shoparticle.*'), 
		);
		$list = POP::exe('[@@:UM shoparticle][@@:QU find:first]',$query);		
		if ($list) {
		    $this->flash('删除失败，请先删除该分类的产品内容！', Router::url('admin/shoparticle/categorieslist/'));
		}
		$query = array('keyid'=>$id);			
		POP::exe('[@@:UM shoparticlecat][@@:QU del]',$query);		
		$this->flash('删除成功！', Router::url('admin/shoparticle/categorieslist/'));		
	}	
	
	public function index() {
        $cat_id = intval(Request::get_args('id'));
        $page = Request::get_args('page');		
		$conditions = array();
        if($cat_id) {
		    $conditions['shoparticle.cat_id'] = $cat_id;
	    }		
        $url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $tb = array('model' => 'shoparticle',
				    'fields' => array('shoparticle.*'),
					'conditions' => $conditions,
					'order' => 'shoparticle.id desc',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
	    $result =  $this->fpage($tb); 			
		$query = array(
			'fields' => array('shoparticlecat.*'), 
		);
		$catlist = POP::exe('[@@:UM shoparticlecat][@@:QU find:all]',$query);		
		$catinfo = array();
        foreach($catlist as $key => $value) {
            $catinfo[$value['shoparticlecat']['id']] = $value['shoparticlecat'];
        }    		
        $this->set('catinfo',$catinfo);	
        $this->set('result',$result);
        $this->set('catlist',$catlist);	
        $this->set('cat_id',$cat_id);			
	}	

	public function add() {
		if (!empty($this->data)) {   
			//$this->data['admin_id'] = $this->my->id;
			$this->data['add_time'] = date("Y-m-d H:i:s");			
			$this->data['shoparticle'] = $this->data;
			$query = $this->data;
			if (POP::exe('[@@:UM shoparticle][@@:QU save]',$query)) {
				$this->flash('更新成功！', Router::url('admin/shoparticle/index'));		
			} else {
				$this->flash('更新失败！', Router::url('admin/shoparticle/index'));
			}					
	    } else {
			$query = array(
				'fields' => array('shoparticlecat.*'), 
			);
			$catlist = POP::exe('[@@:UM shoparticlecat][@@:QU find:all]',$query);	
            $this->set('catlist',$catlist);	
        }
	
	}	
	
	public function edit($id) {
		if (!empty($this->data)) {				
			//$this->data['admin_id'] = $this->my->id;
			$this->data['add_time'] = date("Y-m-d H:i:s");	
            if(!isset($this->data['is_blod']) || empty($this->data['is_blod']) ) {$this->data['is_blod']=0; }	
            if(!isset($this->data['is_link']) || empty($this->data['is_link']) ) {$this->data['is_link']=0; }	
            if(!isset($this->data['is_show']) || empty($this->data['is_show']) ) {$this->data['is_show']=0; }
			$this->data['shoparticle'] = $this->data;			
			$query['keyid'] = $id;
			$query['set']= $this->data;			
			if (POP::exe('[@@:UM shoparticle][@@:QU save]',$query)) {	
				$this->flash('更新成功！', Router::url('admin/shoparticle/index'));		
			} else {
				$this->flash('更新失败！', Router::url('admin/shoparticle/index'));
			}					
	    } else {
			$query = array(
				'fields' => array('shoparticlecat.*'), 
			);
			$catlist = POP::exe('[@@:UM shoparticlecat][@@:QU find:all]',$query);
			$query = array(
				'conditions' => array('shoparticle.id' => $id), 
				'fields' => array('shoparticle.*'), 
			);
			$news_info = POP::exe('[@@:UM shoparticle][@@:QU find:first]',$query);				
			$news_info = $news_info['shoparticle'];			
            $this->set('catlist',$catlist);	
            $this->set('news_info',$news_info);				
        }	
	}

	public function del($id) {
		$query = array('keyid'=>$id);			
		POP::exe('[@@:UM shoparticle][@@:QU del]',$query);	
		$this->flash('删除成功！', Router::url('admin/shoparticle/index/'));		
	}
	
	public function uploadform($is=0) {
	    if($is==1) {
            Loader::import('lib.cupload');		
            $up = new upload('jpg|gif|png',1024,'attach');
            $up->set_dir(APP_ROOT . Param::read('upfiles.path') . '/shopnews/',"{y}/{m}/{d}");
            $fs = $up->execute();
			$rootpath = trim(str_replace(Param::read('wwwrootPatch'),"",Param::read('htdocPatch')),'/') ;
            $rootpath = !empty($rootpath)?	('/'.$rootpath) : '';				
	        $realtxt=$fs[0];			
	        if($realtxt['flag']==1){
		        $fileSrcStr = $rootpath.str_replace(APP_ROOT,"",$realtxt['dir']).$realtxt['name'];
		        echo "<script type='text/javascript'>parent.AddContentImg('$fileSrcStr','1');</script>";
	            $this->actionreturn(1,"",Router::url('admin/shoparticle/uploadform/'));
	        }else if($realtxt['flag']==-1){
		        $this->actionreturn(0,"失败",'-1');
	        }else if($realtxt['flag']==-2){
		        $this->actionreturn(0,"失败",'-1');
	        }		
		}	
	}
    //action动作成功控制函数
	public function actionreturn($success=1,$return_mess="",$activeUrl=""){
	    echo "<script language='javascript'>";
	    $setUrl = '';
	    if($activeUrl!=''){
		    $setUrl = $activeUrl;
	    } else {
		    $setUrl = '-1';
	    }
	    if(trim($return_mess)!=''){
		    echo "ShowMessageBox('$return_mess','$setUrl');";
	    }else{
		    echo "location.href='$setUrl'";
	    }
	    echo "</script>";
		$this->render("actionreturn");
		exit;
    }	
}
?> 